# -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: s; -*- 
#
# Copyright (C) 2008 - Grzes Furga
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.


import rhythmdb, rb
import gtk, gobject
from warnings import warn

#from CoverArtDatabase import CoverArtDatabase
from flow_widget import CoverFlowWidget

import sys
def deb(s):
    sys.stderr.write(s)

    
class CoverFlowPlugin (rb.Plugin):
    def __init__ (self):
        rb.Plugin.__init__ (self)
        
        
    def activate (self, shell):
        self.shell = shell
        self.player = shell.get_player ()
        self.db     = shell.get_property ("db")
        self.source = shell.get_property('selected_source')
        
        
        self.conn_signals = [ 
            (self.player,   self.player.connect('playing-song-changed', self.playing_entry_changed)),
            (self.source,   self.source.connect('filter-changed', self.source_changed)),
            (self.shell,    self.shell.connect('notify::selected-source', self.source_changed)),
            ]
		
	
		self.flow_widget = CoverFlowWidget ()
        self.shell.add_widget (self.flow_widget, rb.SHELL_UI_LOCATION_MAIN_TOP)
        
        self.flow_widget.show()
        
       
        # until we have the tracklist send us the signal, we'll manualy gather songs on play        
        def first_time_load_songs():
            self.source.emit("filter-changed")
            return False
            
        gobject.timeout_add(2000, first_time_load_songs) # give the library some time to init
        
    def deactivate (self, shell):
        self.shell = None
        sp = shell.get_player()
        db = shell.get_property("db")
        
        for t,sig in self.conn_signals: t.disconnect(sig)
 

        shell.remove_widget (self.flow_widget, rb.SHELL_UI_LOCATION_MAIN_TOP)
        self.flow_widget = None

       
    def playing_entry_changed (self, sp, entry):
        """When playing another song. Go to the proper cover. It *should* be on currently loaded list""" 
        if not entry: return # when changing sources this sometimes get fired with a None entry
        
        self.flow_widget.goto(self.db.entry_get(entry, rhythmdb.PROP_ENTRY_ID))
        
        
    def source_changed(self, *args):
        self.source = self.shell.props.selected_source
        dbget = self.db.entry_get    
        songs = []
        for num, row in enumerate(self.source.props.query_model):
            entry = row[0]

            entryid = dbget(entry, rhythmdb.PROP_ENTRY_ID)        
            title = dbget(entry, rhythmdb.PROP_TITLE)
            album = dbget(entry, rhythmdb.PROP_ALBUM)
            artist = dbget(entry, rhythmdb.PROP_ARTIST)
            songs.append((entryid,
                          #entry.get_playback_uri(),
                          artist, album, title))

        self.flow_widget.load_covers(songs)
        now = self.player.get_playing_entry()            
        if now: self.playing_entry_changed(self.player, now)
            

